package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysWorks;
import com.ruoyi.system.service.ISysWorksService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "作品")
@RestController
@RequestMapping("/system/works")
public class SysWorksController extends BaseController {
    @Autowired
    private ISysWorksService sysWorksService;

    @Operation(summary = "获取作品列表票数")
    @GetMapping("/piaoshu")
    public TableDataInfo piaoshu(SysWorks sysWorks) {
        if (sysWorks.getWorksUserId() != null){
            List<SysWorks> list = sysWorksService.piaoshu(sysWorks.getWorksUserId().toString());
            return getDataTable(list);
        }
        startPage();
        List<SysWorks> list = sysWorksService.piaoshu(null);
        return getDataTable(list);
    }

    @Operation(summary = "获取作品列表时间")
    @GetMapping("/shijian")
    public TableDataInfo shijian(SysWorks sysWorks) {
        startPage();
        List<SysWorks> list = sysWorksService.shijian();
        return getDataTable(list);
    }

    /**
     * 查询作品列表
     */
//    @PreAuthorize("@ss.hasPermi('system:works:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWorks sysWorks) {
        startPage();
        List<SysWorks> list = sysWorksService.selectWorksList(sysWorks);
        return getDataTable(list);
    }

    /**
     * 导出作品列表
     */
//    @PreAuthorize("@ss.hasPermi('system:works:export')")
    @Log(title = "作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWorks sysWorks) {
        List<SysWorks> list = sysWorksService.selectSysWorksList(sysWorks);
        ExcelUtil<SysWorks> util = new ExcelUtil<SysWorks>(SysWorks.class);
        util.exportExcel(response, list, "作品数据");
    }

    /**
     * 获取作品详细信息
     */
    @GetMapping(value = "/{worksKey}")
    public AjaxResult getInfo(@PathVariable("worksKey") String worksKey) {
        return success(sysWorksService.selectSysWorksByWorksKey(worksKey));
    }

    /**
     * 新增作品
     */
//    @PreAuthorize("@ss.hasPermi('system:works:add')")
    @Log(title = "作品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWorks sysWorks) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        List<SysWorks> piaoshu = sysWorksService.piaoshu(userId.toString());
        if (piaoshu.size()>=3){
            return AjaxResult.error("每人最多可上传3个作品！");
        }
        return toAjax(sysWorksService.insertSysWorks(sysWorks));
    }

    /**
     * 修改作品
     */
//    @PreAuthorize("@ss.hasPermi('system:works:edit')")
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWorks sysWorks) {
        return toAjax(sysWorksService.updateSysWorks(sysWorks));
    }

    /**
     * 删除作品
     */
//    @PreAuthorize("@ss.hasPermi('system:works:remove')")
    @Log(title = "作品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{worksKeys}")
    public AjaxResult remove(@PathVariable String[] worksKeys) {
        return toAjax(sysWorksService.deleteSysWorksByWorksKeys(worksKeys));
    }
}
